package com.wangsd.web.coupon.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wangsd.common.basicbean.Result;
import com.wangsd.web.MyController;
import com.wangsd.web.coupon.entity.Coupon;
import com.wangsd.web.coupon.entity.Store;
import com.wangsd.web.coupon.service.CouponService;
import com.wangsd.web.coupon.service.StoreService;
import com.wangsd.web.sys.entity.SysUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.List;
import java.util.stream.Collectors;

import static com.wangsd.common.enums.ResultCode.PRARM_NOT_NULL;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author generator
 * @since 2019-09-24
 */
@Api(value = "coupon", tags = "优惠券管理")
@RestController
@RequestMapping("/coupon/coupon")
public class CouponController extends MyController {

    @Autowired
    CouponService couponService;

    @Autowired
    StoreService storeService;


    @GetMapping("/selectById/{id}")
    @ApiOperation(value = "查询单条优惠券信息", notes = "查询单条优惠券信息", httpMethod = "GET", response = Result.class)
    public Result selectById(@PathVariable String id) {
        Coupon byId = couponService.getById(id);
        return Result.success(byId);
    }

    @PostMapping("/selectList/{pageNum}/{pageSize}")
    @ApiOperation(value = "查询优惠券列表", notes = "查询优惠券列表", httpMethod = "POST")
    public Result selectList(@PathVariable Integer pageNum,
                             @PathVariable Integer pageSize,
                             @RequestBody @ApiParam(name = "coupon", value = "传入json格式") Coupon coupon) {
        SysUser loginUser = getUserInfo();

        String code = loginUser.getCode();
        if (coupon.getCode() != null) {
            code = coupon.getCode();
        }

        IPage<Coupon> resultList = couponService.selectPage(code, pageNum, pageSize, coupon);
        if (resultList != null && resultList.getSize() > 0) {
            List<Coupon> records = resultList.getRecords();
            if (records != null && records.size() > 0) {
                for (Coupon coupons : records) {
                    List<Store> storeList = storeService.selectByCouponId(coupons.getId());
                    if (storeList != null && storeList.size() > 0) {
                        List<String> collect = storeList.stream().map(Store::getCode).collect(Collectors.toList());
                        coupons.setStoreCodes(collect);
                    }
                }
            }
        }
        return Result.success(resultList);
    }

    @GetMapping("/delete/{id}")
    @ApiOperation(value = "删除优惠券", notes = "删除优惠券", httpMethod = "GET")
    public Result delete(@PathVariable String id) {
        couponService.removeById(id);
        return Result.success();
    }


    @PostMapping("/addOrUpdate")
    @ApiOperation(value = "添加或修改优惠券", notes = "添加或修改优惠券", httpMethod = "POST")
    public Result addOrUpdate(@RequestBody Coupon coupon) {

        if (coupon.getCompanyId() == null) {
            return new Result(PRARM_NOT_NULL.getCode(), "商圈（公司）" + PRARM_NOT_NULL.getMessage());
        }

        if (coupon.getName() == null) {
            return new Result(PRARM_NOT_NULL.getCode(), "名称" + PRARM_NOT_NULL.getMessage());
        }

        if (coupon.getStoreIds() == null || coupon.getStoreIds().size() <= 0) {
            return new Result(PRARM_NOT_NULL.getCode(), "门店" + PRARM_NOT_NULL.getMessage());
        }
        if (coupon.getCouponStockId() == null) {
            return new Result(PRARM_NOT_NULL.getCode(), "批次号" + PRARM_NOT_NULL.getMessage());
        }
        if (coupon.getCouponPicUrl() == null) {
            return new Result(PRARM_NOT_NULL.getCode(), "优惠券图片" + PRARM_NOT_NULL.getMessage());
        }
        if (coupon.getId() == null) {
            Coupon old = couponService.selectRepeat(coupon.getCouponStockId(), coupon.getId());
            if (old != null) {
                return Result.fail("批次号重复！");
            }
        }

        couponService.addOrUpdate(coupon);
        return Result.success();
    }

    @ApiOperation(value = "优惠券上下架", notes = "优惠券上下架", httpMethod = "POST")
    @PostMapping("/updateState")
    public Result updateState(@RequestBody Coupon coupon) {
        if (coupon.getState() == null) {
            return new Result(PRARM_NOT_NULL.getCode(), "state" + PRARM_NOT_NULL.getMessage());
        }
        if (coupon.getId() == null) {
            return new Result(PRARM_NOT_NULL.getCode(), "id" + PRARM_NOT_NULL.getMessage());
        }
        couponService.updateState(coupon);
        return Result.success();
    }

    @ApiOperation(value = "优惠券排序", notes = "优惠券排序", httpMethod = "POST")
    @PostMapping("/updateSort")
    public Result updateSort(@RequestBody Coupon coupon) {
        if (coupon.getSort() == null) {
            return new Result(PRARM_NOT_NULL.getCode(), "sort" + PRARM_NOT_NULL.getMessage());
        }
        if (coupon.getId() == null) {
            return new Result(PRARM_NOT_NULL.getCode(), "id" + PRARM_NOT_NULL.getMessage());
        }
        couponService.updateById(coupon);
        return Result.success();
    }

    @ApiOperation(value = "通过优惠券id查询被选中的门店", notes = "通过优惠券id查询被选中的门店", httpMethod = "GET", response = Result.class)
    @RequestMapping(value = "/selectStoreById/{id}", method = RequestMethod.GET)
    public Result selectStoreById(@ApiParam(name = "id", value = "优惠券id") @PathVariable String id) {
        List<String> result = couponService.selectStoreById(id);
        return Result.success(result);
    }

}

